using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ChessGame : MonoBehaviour {

    // Entities and their states / Model
    private static int player;
    private static int count;
    private int winner;
    private int[,] chessBoard = new int[10, 10];

    private int current_put_x;
    private int current_put_y;
    private int chess_size;
    // System Handlers
    void Start () {
        Init();
    }

    // View to render entities / models
    // Here! you cannot modify model directly, use components/controls to do it
    void OnGUI() {
        GUI.Box(new Rect(155, 0, 630, 630), "");
        if (GUI.Button(new Rect(800, 315, 100, 30), "Restart")) Init();
        if (!GameOver()) {
            for (int i = 0; i < 10; i++) {
                for (int j = 0; j < 10; j++) {
                    if (chessBoard[i, j] == 0 && GUI.Button(new Rect(155 + j * 63, 0 + i * 63, 63, 63), "")) {
                        PutChess(i,j);
                    }
                    else if (chessBoard[i, j] == 1) GUI.Button(new Rect(155 + j * 63, 0 + i * 63, 63, 63), "O");
                    else if (chessBoard[i, j] == 2) GUI.Button(new Rect(155 + j * 63, 0 + i * 63, 63, 63), "X");
                }
            }
        }
        else {
            if (winner != 0)
                GUI.Box(new Rect(260, 50, 200, 200), "\n\n\n\n\nCongratulations!\n Player "+winner+" has won.");
            else
                GUI.Box(new Rect(260, 50, 200, 200), "\n\n\n\n\n\nThis is a draw!");
        }
    }

    // Components /controls
    void Init() {
        player = 1;
        winner = 0;
        count = 0;
        chess_size = 10;
        for(int i = 0; i < 10; i++)
            for(int j = 0; j < 10; j++)
                chessBoard[i, j] = 0;
    }

    void PutChess(int i,int j) {
        chessBoard[i, j] = player;
        player = 3 - player;
        count++;       
        current_put_x = i;
        current_put_y = j;
    }

    bool GameOver() {

        if(count_left()+count_right()+1>=5){
            winner = chessBoard[current_put_x,current_put_y];
        }
        else if(count_up()+count_down()+1>=5){
            winner = chessBoard[current_put_x,current_put_y];
        }
        else if(count_left_up()+count_right_down()+1>=5){
            winner = chessBoard[current_put_x,current_put_y];
        }
        else if(count_left_down()+count_right_up()+1>=5){
            winner = chessBoard[current_put_x,current_put_y];
        }

        if (count < 100 && winner == 0) return false;

        return true;
    }

    int count_left(){
        int count = 0;
        for(int i=1; i<=5; i++){
            if(current_put_x-i<0){
                return count;
            }
            if(chessBoard[current_put_x, current_put_y] == chessBoard[current_put_x-i, current_put_y]){
                count++;
            }
            else{
                return count;
            }
        }
        return count;
    }
    int count_right(){
        int count = 0;
        for(int i=1; i<=5; i++){
            if(current_put_x+i>9){
                return count;
            }
            if(chessBoard[current_put_x, current_put_y] == chessBoard[current_put_x+i, current_put_y]){
                count++;
            }
            else{
                return count;
            }
        }
        return count;
    }
    int count_up(){
        int count = 0;
        for(int i=1; i<=5; i++){
            if(current_put_y+i>9){
                return count;
            }
            if(chessBoard[current_put_x, current_put_y] == chessBoard[current_put_x, current_put_y+i]){
                count++;
            }
            else{
                return count;
            }
        }
        return count;
    }
    int count_down(){
        int count = 0;
        for(int i=1; i<=5; i++){
            if(current_put_y-i<0){
                return count;
            }
            if(chessBoard[current_put_x, current_put_y] == chessBoard[current_put_x, current_put_y-i]){
                count++;
            }
            else{
                return count;
            }
        }
        return count;
    }
    int count_left_up(){
        int count = 0;
        for(int i=1; i<=5; i++){
            if( current_put_x-i<0 || current_put_y+i>9 ){
                return count;
            }
            if(chessBoard[current_put_x, current_put_y] == chessBoard[current_put_x-i, current_put_y+i]){
                count++;
            }
            else{
                return count;
            }
        }
        return count;
    }
    int count_right_down(){
        int count = 0;
        for(int i=1; i<=5; i++){
            if( current_put_x+i>9 || current_put_y-i<0 ){
                return count;
            }
            if(chessBoard[current_put_x, current_put_y] == chessBoard[current_put_x+i, current_put_y-i]){
                count++;
            }
            else{
                return count;
            }
        }
        return count;
    }
    int count_right_up(){
        int count = 0;
        for(int i=1; i<=5; i++){
            if( current_put_x+i>9 || current_put_y+i>9 ){
                return count;
            }
            if(chessBoard[current_put_x, current_put_y] == chessBoard[current_put_x+i, current_put_y+i]){
                count++;
            }
            else{
                return count;
            }
        }
        return count;
    }
    int count_left_down(){
        int count = 0;
        for(int i=1; i<=5; i++){
            if( current_put_x-i<0 || current_put_y-i<0 ){
                return count;
            }
            if(chessBoard[current_put_x, current_put_y] == chessBoard[current_put_x-i, current_put_y-i]){
                count++;
            }
            else{
                return count;
            }
        }
        return count;
    }
}

